package com.example.testshapeabletextview

import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.shape.*

class MaterialShapeDrawableAct : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        //-=------------------------------------------------------------
        val appearanceModel = ShapeAppearanceModel.builder()
            .setAllCornerSizes(ScreenUtil.dp2pxF(applicationContext, 6F))
            .setAllCorners(RoundedCornerTreatment())
            .setBottomEdge(OffsetEdgeTreatment(  //位移
                TriangleEdgeTreatment(ScreenUtil.dp2pxF(applicationContext, 6F), false), //三角状
                ScreenUtil.dp2pxF(applicationContext, 40F) //中间往右位移40dp
            ))
        .build()

        val drawable = MaterialShapeDrawable(appearanceModel).apply {
            setTint(Color.parseColor("#FA4B05"))
            paintStyle = Paint.Style.FILL
        }

        val tv_tip = findViewById<TextView>(R.id.tv_2)
        val lltv = findViewById<LinearLayout>(R.id.ll_tv)
        tv_tip.background = drawable

        //-=------------------------------------------------------------
        val dp2pxF = ScreenUtil.dp2pxF(applicationContext, 5f)
        val shapeable = ShapeAppearanceModel.builder()
            .setAllCornerSizes(dp2pxF)
            .setAllCorners(CutCornerTreatment())
            .setAllEdges(TriangleEdgeTreatment(dp2pxF, true))  //true表示三角形尖角向内
            .build()
        val background = MaterialShapeDrawable(shapeable)
            .apply {
                setTint(Color.parseColor("#FA4B05"))
                paintStyle = Paint.Style.FILL
            }
        val tv_tip3 = findViewById<TextView>(R.id.tv_3)
        tv_tip3.background = background
        //-=------------------------------------------------------------
        val background2 = MaterialShapeDrawable(
            ShapeAppearanceModel.builder()
                .setAllCornerSizes(dp2pxF)
                .setAllCorners(CutoutCornersTreatment(dp2pxF))
                .setAllEdges(CurvedEdgeTreatment(dp2pxF))
                .build()
        ).apply {
            setTint(Color.parseColor("#FA4B05"))
            paintStyle = Paint.Style.FILL
        }
        val tv_tip4 = findViewById<TextView>(R.id.tv_4)
        tv_tip4.background = background2

        //-=------------------------------------------------------------
        val background3 = MaterialShapeDrawable(
            ShapeAppearanceModel.builder()
                .setAllCornerSizes(dp2pxF)
                .setAllCorners(CutoutCornersTreatment(dp2pxF, false))
                .setAllEdges(CurvedEdgeTreatment(dp2pxF, false))
                .build()).apply {
            setTint(Color.parseColor("#FA4B05"))
            paintStyle = Paint.Style.FILL
        }

        val tv_tip5 = findViewById<TextView>(R.id.tv_5)
        tv_tip5.background = background3
    }

    class CutoutCornersTreatment(val size : Float, val inside : Boolean=true) : CornerTreatment() {
        override fun getCornerPath(
            shapePath: ShapePath, angle: Float, interpolation: Float, radius: Float) {
//            super.getCornerPath(shapePath, angle, interpolation, radius)
            shapePath.reset(0.0f, size * interpolation)
            shapePath.lineTo(size * interpolation, if(inside)size * interpolation else -size * interpolation)
            shapePath.lineTo(size * interpolation, 0f)
        }
    }

    class CurvedEdgeTreatment(val size : Float, val inside : Boolean=true) : EdgeTreatment() {
        override fun getEdgePath(length: Float, center: Float,
            interpolation: Float, shapePath: ShapePath) {
//            super.getEdgePath(length, center, interpolation, shapePath)
            //贝塞尔曲线
            shapePath.quadToPoint(length / 2f,
                if(inside)size * interpolation else (- size * interpolation),
                length, 0f)
        }
    }
}